class Solution {
public:
    string convert(string s, int numRows) {
        string ret;
        if (numRows == 1) return s;
        int d = 2 * numRows - 2;
        for (int i = 0; i < s.size(); i += d) {
            ret += s[i];
        }
        for (int k = 1; k < numRows - 1; k++) {
            for (int i = k, j = d - k; i < s.size() || j < s.size(); i += d, j += d) {
                if (i < s.size()) ret += s[i];
                if (j < s.size()) ret += s[j];
            }
        }
        for (int j = numRows - 1; j < s.size(); j += d) {
            ret += s[j];
        }
        return ret;
    }
};